/*
 * InsertBus.java
 *
 * Created on April 1, 2007, 3:39 PM
 */

package busticketvendingsystem;
import javax.swing.*;
/**
 *
 * @author  Administrator
 */
public class InsertBus extends javax.swing.JDialog {
    ConnectDatabase cd=new ConnectDatabase();
    /** Creates new form InsertBus */
    public InsertBus(JDialog parent, boolean modal) {
        super(parent,modal);
        initComponents();
        setLocationRelativeTo(parent);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        busnumber = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        uptime = new javax.swing.JTextField();
        downtime = new javax.swing.JTextField();
        seats = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        insert = new javax.swing.JButton();
        finish = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Insert a bus");
        setResizable(false);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Enter data"));
        jLabel2.setText("Bus number :");

        busnumber.setToolTipText("Enter bus number here");

        jLabel3.setText("Up schedule time :");

        jLabel4.setText("Down schedule time :");

        jLabel5.setText("No. of seats :");

        uptime.setToolTipText("Enter up schedule time(When it leaves Garia)");

        downtime.setToolTipText("Enter down schedule time(When it leaves Howrah)");

        seats.setToolTipText("Enter the number of seats in the bus");
        seats.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seatsActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jLabel4)
                    .add(jLabel5)
                    .add(jLabel2))
                .add(7, 7, 7)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(busnumber)
                    .add(seats)
                    .add(downtime)
                    .add(uptime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(75, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(busnumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(uptime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(downtime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5)
                    .add(seats, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/busticketvendingsystem/resources/images/ins_bus.gif")));
        jLabel1.setText("Insert a bus");

        insert.setText("Insert");
        insert.setToolTipText("Insert bus to database");
        insert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertActionPerformed(evt);
            }
        });

        finish.setText("Finish");
        finish.setToolTipText("Finish entering busses");
        finish.setEnabled(false);
        finish.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finishActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.setToolTipText("Cancel this dialog");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/busticketvendingsystem/resources/images/help_icon_small.gif")));
        jButton1.setText("Help");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(insert)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(finish)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(8, 8, 8)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancel)
                    .add(finish)
                    .add(insert)
                    .add(jButton1))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new HelpWindow(InsertBus.this,true,"ins_bus.htm").setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void seatsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seatsActionPerformed
// TODO add your handling code here:
        Insert();
    }//GEN-LAST:event_seatsActionPerformed

    private void finishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishActionPerformed
// TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_finishActionPerformed

    private void insertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertActionPerformed
// TODO add your handling code here:
        Insert();
    }//GEN-LAST:event_insertActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
// TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_cancelActionPerformed
    private void Insert(){
        String bnumber="";
        String up="";
        String down="";
        int seat,c=0;
        boolean type=true;
        boolean type1=false;
        try{
            bnumber=busnumber.getText();
            up=uptime.getText();
            down=downtime.getText();
            String s=seats.getText();
            seat=Integer.parseInt(s);
            String busno[]=cd.getBus_number_byUpSchedule();
            for(int i=0;i<busno.length;i++){
                if(busno[i].equals(bnumber)){
                    type=false;
                }
            }
            String upt[]=cd.getUpSchedule();
            for(int i=0;i<upt.length;i++){
                if(upt[i].equals(up)){
                    type=false;
                }
            }
            String downt[]=cd.getDownSchedule();
            for(int i=0;i<downt.length;i++){
                if(downt[i].equals(down)){
                    type=false;
                }
            }
            if(!type){
                JOptionPane.showMessageDialog(null,"Wrong Input!\nCheck your entry carefully","ERROR",JOptionPane.ERROR_MESSAGE);
                c=1;
            }
            else{
                type1=cd.insertBus(bnumber,up,down,seat);
                
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null,"Wrong Input!\nCheck your entry carefully","ERROR",JOptionPane.ERROR_MESSAGE);
            c=1;
        }
        if(!type1&&c==0){
                JOptionPane.showMessageDialog(null,"Wrong Input!\nCheck your entry carefully","ERROR",JOptionPane.ERROR_MESSAGE);
            }
        else if(type1&&c==0){
            JOptionPane.showMessageDialog(null,"New bus have been inserted");
            finish.setEnabled(true);
            busnumber.setText("");
            uptime.setText("");
            downtime.setText("");
            seats.setText("");
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InsertBus(null,true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField busnumber;
    private javax.swing.JButton cancel;
    private javax.swing.JTextField downtime;
    private javax.swing.JButton finish;
    private javax.swing.JButton insert;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField seats;
    private javax.swing.JTextField uptime;
    // End of variables declaration//GEN-END:variables
    
}
